package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.entity.dto.FibSealDeptDTO;
import com.bosssoft.entity.vo.FibSealDeptVO;
import com.bosssoft.enums.EleSerCode;
import com.bosssoft.exception.DaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.entity.po.FibSealDeptPO;
import com.bosssoft.mapper.FibSealDeptMapper;
import com.bosssoft.service.FibSealDeptService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


/**
 * @author Li kexi
 * Created on 2021/8/16.14:36
 */
@Slf4j
@Service
public class FibSealDeptServiceImpl extends ServiceImpl<FibSealDeptMapper, FibSealDeptPO> implements FibSealDeptService {

    @Override
    public void updateDeleted(FibSealDeptDTO fibSealDeptDTO) {
        FibSealDeptPO fibSealDeptPO = new FibSealDeptPO();
        BeanUtils.copyProperties(fibSealDeptDTO, fibSealDeptPO);
        //id判空
        if (StringUtils.isEmpty(fibSealDeptPO.getSealId())||
            StringUtils.isEmpty(fibSealDeptPO.getDeptId())) {
            log.info("更新印章id或者部门id为空,in sealDept_updateDeleted方法");
            throw DaoException.error(EleSerCode.SEAL_OR_DEPT_IS_NULL);
        }
        //更新人判空
        if (StringUtils.isEmpty(fibSealDeptPO.getUpdateBy())) {
            throw DaoException.error(EleSerCode.UPDATEBY_NAME_NOT_FOUND);
        }
        QueryWrapper<FibSealDeptPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seal_id", fibSealDeptPO.getSealId());
        queryWrapper.eq("dept_id", fibSealDeptPO.getDeptId());
        FibSealDeptPO sqlPO = baseMapper.selectOne(queryWrapper);
        //数据库记录判空
        if (sqlPO == null) {
            log.info("前台传id值为"+fibSealDeptPO.getSealId()+"根据code查找,数据库没有记录,in sealDept_updateDeleted方法");
            throw DaoException.error(EleSerCode.SEAL_DEPT_NOT_FOUNDS);
        }
        //该条记录已禁用
        if (StringUtils.isEmpty(sqlPO.getDeleted())) {
            log.info("印章id值为"+fibSealDeptPO.getSealId()+"并且部门id值为"+fibSealDeptPO.getDeptId()+"的记录已被禁用,in sealDept_updateDeleted方法");
            throw DaoException.error(EleSerCode.SEAL_DEPT_IS_DELETED);
        }
        fibSealDeptPO.setDeleted((byte) -1);
        baseMapper.updateById(fibSealDeptPO);
    }

    @Override
    public int updateBatch(List<FibSealDeptPO> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FibSealDeptPO> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FibSealDeptPO> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int insertOrUpdate(FibSealDeptDTO record) {
        FibSealDeptPO fibSealDeptPO = new FibSealDeptPO();
        BeanUtils.copyProperties(record, fibSealDeptPO);
        //查重
        QueryWrapper<FibSealDeptPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seal_id", fibSealDeptPO.getSealId());
        FibSealDeptPO sqlPO = baseMapper.selectOne(queryWrapper);
        if (sqlPO == null) {
            throw DaoException.error(EleSerCode.SEAL_DEPT_IS_HAVE);
        }
        fibSealDeptPO.setVersion(1);
        return baseMapper.insertOrUpdate(fibSealDeptPO);
    }

    @Override
    public int insertOrUpdateSelective(FibSealDeptPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    @Transactional(rollbackFor = DaoException.class)
    public void sealDeleteList(List<FibSealDeptVO> fibSealDeptVOList) {
        for (FibSealDeptVO fibSealDeptVO : fibSealDeptVOList) {
            //必须有修改人
            if (StringUtils.isEmpty(fibSealDeptVO.getUpdateBy())) {
                throw DaoException.error(EleSerCode.UPDATEBY_NAME_NOT_FOUND);
            }
            baseMapper.deleteById(fibSealDeptVO.getSealId());
        }
    }

    //根据部门id查询所有印章
    @Override
    public List<FibSealDeptPO> getSealBySealId(Long deptId) {
        QueryWrapper<FibSealDeptPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        return baseMapper.selectList(queryWrapper);
    }
}
